package cn.hnu.qq.frame;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.imageio.ImageIO;
import javax.swing.JDialog;
import javax.swing.JTabbedPane;

import org.apache.log4j.Logger;

import cn.hnu.qq.client.controller.CrowdController;
import cn.hnu.qq.client.controller.ListenClientMessageThread;
import cn.hnu.qq.client.controller.ListenServerMessageThread;
import cn.hnu.qq.client.event.MessageEvent;
import cn.hnu.qq.client.listener.MessageListener;
import cn.hnu.qq.client.panel.CrowdChartPanel;
import cn.hnu.qq.entity.Crowd;
import cn.hnu.qq.entity.message.CrowdChartMessage;
import cn.hnu.qq.entity.message.Message;
import cn.hnu.qq.entity.message.NewMemberMessage;
import cn.hnu.qq.global.MessageType;

public class CrowdChartDialog extends JDialog implements MessageListener{

	private static CrowdChartDialog instance = new CrowdChartDialog(); 
	
	private Map<String,Integer> openedPanel = new HashMap<String,Integer>();
	
	private JTabbedPane tabbedPane;
	
	private int count = 0;
	
	private CrowdController controller = CrowdController.getInstance();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			CrowdChartDialog dialog = new CrowdChartDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	private CrowdChartDialog() {
		try {
			setIconImage(ImageIO.read(new File("src/main/java/image/xiaohui.jpg")));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		setBounds(100, 100, 450, 300);
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		getContentPane().add(tabbedPane, BorderLayout.CENTER);
		ListenClientMessageThread.getInstance().addMessageListener(this);
		ListenServerMessageThread.getInstance().addMessageListener(this);
	}

	public static CrowdChartDialog getIntance(){
		return instance;
	}
	
	public CrowdChartPanel createChartTab(final Crowd crowd) {
		CrowdChartPanel ccp = null;
		if(openedPanel.get(crowd.getCrowdName()) != null){
			tabbedPane.setSelectedIndex(openedPanel.get(crowd.getCrowdName()));
			controller.queryCrowdIpInfo(crowd);
		}else{
			ccp = new CrowdChartPanel();
			ListenClientMessageThread.getInstance().addMessageListener(ccp);
			tabbedPane.add(ccp,count);
			tabbedPane.setTitleAt(count, crowd.getCrowdName());
			tabbedPane.setSize(ccp.getWidth(),ccp.getHeight()+25);
			openedPanel.put(crowd.getCrowdName(), count);
			tabbedPane.setSelectedIndex(count++);
			controller.queryCrowdIpInfo(crowd);
			
			this.setSize(tabbedPane.getWidth(), tabbedPane.getHeight()+55);
			validate();
		}
		return ccp;
	}

	public void messageArrived(MessageEvent event) {
		Message message = event.getSource();
		if(message.getMessageType() == MessageType.CROWD_CHART){
			CrowdChartMessage cm = (CrowdChartMessage) message;
			if(openedPanel.get(cm.getCrowdName()) == null){
				Set<Crowd> crowds = controller.getCurUser().getCrowds();
				for(Crowd c:crowds){
					if(c.getCrowdName().equals(cm.getCrowdName())){
						createChartTab(c).insertMessage(cm);
						break;
					}
				}
			}else{
				tabbedPane.setSelectedIndex(openedPanel.get(cm.getCrowdName()));
			}
		}
		if(message.getMessageType() == MessageType.NEW_MEMBER){
			NewMemberMessage nmm = (NewMemberMessage) message;
			Logger.getLogger(CrowdChartDialog.class).debug("CrowdChartDialog 处理NEW_MEMBER："+message);
			if(openedPanel.get(nmm.getCrowd()) != null){
				CrowdChartPanel ccp = (CrowdChartPanel) tabbedPane.getComponentAt((openedPanel.get(nmm.getCrowd())));
				ccp.addNewMember(nmm.getUser());
			}
		}
	}

	public void closeChartTab(Crowd c) {
		Integer index = openedPanel.get(c.getCrowdName());
		if(index != null){
			tabbedPane.removeTabAt(index);
			openedPanel.remove(c.getCrowdName());
			count--;
			if(count == 0){
				this.dispose();
			}
		}
	}

}
